
#ifndef UPDATE_DOWNLOAD_MANAGER_H
#define UPDATE_DOWNLOAD_MANAGER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class DownloadManager : public QObject
{
    Q_OBJECT
public:
    explicit DownloadManager(QObject *parent = 0);
    ~DownloadManager() override;
    // 下载文件
    void downloadFile(const QString &url,const QString &fileName);
    // 设置是否支持断点续传
    void setDownInto(const bool &isSupportBreakPoint);
    // 停止下载
    void stopDownload();
    // 关闭下载
    void closeDownload();
    // 获取下载url
    QString getDownloadUrl();
    // 重置参数;
    void reset();

private:
    // 停止下载工作;
    void stopWork();
    // 删除文件;
    void removeFile(const QString &fileName);

signals:
    void signalDownloadProcess(qint64, qint64);
    void signalReplyFinished(int);
    void signalDownloadError();

private slots:
    void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
    void onReadyRead();
    void onFinished();
    void onError(QNetworkReply::NetworkError code);

private:
    QNetworkAccessManager *network_manager_{nullptr};
    QNetworkReply         *reply_{nullptr};
    QUrl                   url_{};
    QString                file_name_{};
    bool                   is_support_break_point_{false};

    qint64                 bytes_received_;
    qint64                 bytes_total_;
    qint64                 bytes_current_received_;

    bool                   is_stop_{true};
};

#endif //UPDATE_DOWNLOAD_MANAGER_H
